package org.korecky.gnotedigitalorganizer;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextPane;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import net.miginfocom.swing.MigLayout;
import org.korecky.gnotedigitalorganizer.settings.ApplicationSettings;
import org.korecky.gnotedigitalorganizer.settings.ApplicationSettingsHelper;

public class FirstRunWizard extends JDialog {
    private AppContext appContext;
    private JPanel contentPane;
    private JLabel lblNewLabel;
    private JLabel lblNewLabel_1;
    private JPanel panel;
    private JLabel lblNewLabel_2;
    private JPanel panel_1;
    private JLabel lblNewLabel_3;
    private JButton btnSelect;
    private JPanel panel_2;
    private JButton btnFinish;
    private JTextPane txpSelectedLibraryPath;
    private final String defaultLibraryPath = System.getProperty("user.home") + System.getProperty("file.separator") + "G-Note Library";
    private JPanel panel_3;

    /**
     * Create the frame.
     */
    public FirstRunWizard(AppContext appContext) {
	this.appContext = appContext;
	setLocationByPlatform(true);
	setModalityType(ModalityType.TOOLKIT_MODAL);
	this.appContext.setAppSettings(null);
	setModal(true);
	initComponents();
	txpSelectedLibraryPath.getDocument().addDocumentListener(new DocumentListener() {
	    @Override
	    public void removeUpdate(DocumentEvent e) {
		validateLibraryPath();
	    }

	    @Override
	    public void insertUpdate(DocumentEvent e) {
		validateLibraryPath();
	    }

	    @Override
	    public void changedUpdate(DocumentEvent e) {
		validateLibraryPath();
	    }
	});
	txpSelectedLibraryPath.setText(defaultLibraryPath);
    }

    private void initComponents() {
	setTitle("First run wizard");
	setIconImage(Toolkit.getDefaultToolkit().getImage(FirstRunWizard.class.getResource("/org/korecky/gnotedigitalorganizer/resources/appIcons/icon64.png")));
	setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
	setBounds(100, 100, 635, 346);
	this.contentPane = new JPanel();
	this.contentPane.setBackground(Color.WHITE);
	this.contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
	this.contentPane.setLayout(new BorderLayout(0, 0));
	setContentPane(this.contentPane);

	this.lblNewLabel = new JLabel("<html><br/><b>Welcome to G-Note Digital Organizer</b><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Application for your Genius G-Note device.<br/><br/><br/></html>");
	this.lblNewLabel.setFont(this.lblNewLabel.getFont().deriveFont(Font.PLAIN));
	this.contentPane.add(this.lblNewLabel, BorderLayout.NORTH);

	this.lblNewLabel_1 = new JLabel("");
	this.lblNewLabel_1.setIcon(new ImageIcon(FirstRunWizard.class.getResource("/org/korecky/gnotedigitalorganizer/resources/appIcons/icon128.png")));
	this.contentPane.add(this.lblNewLabel_1, BorderLayout.WEST);

	this.panel = new JPanel();
	this.panel.setBackground(Color.WHITE);
	this.contentPane.add(this.panel, BorderLayout.CENTER);
	this.panel.setLayout(new MigLayout("", "[grow,fill]", "[][]"));

	this.panel_1 = new JPanel();
	this.panel_1.setBackground(Color.WHITE);
	this.panel.add(this.panel_1, "cell 0 0,grow");
	this.panel_1.setLayout(new MigLayout("", "[grow,fill][]", "[14px][grow]"));

	this.lblNewLabel_3 = new JLabel("Select library path");
	this.lblNewLabel_3.setBackground(Color.WHITE);
	this.panel_1.add(this.lblNewLabel_3, "cell 0 0,alignx left,aligny top");

	this.btnSelect = new JButton("Change");
	this.btnSelect.addActionListener(new ActionListener() {
	    public void actionPerformed(ActionEvent arg0) {
		btnSelectActionPerformed(arg0);
	    }
	});

	this.panel_3 = new JPanel();
	this.panel_3.setBorder(new LineBorder(new Color(0, 0, 0)));
	this.panel_3.setBackground(Color.WHITE);
	this.panel_1.add(this.panel_3, "cell 0 1,grow");
	this.panel_3.setLayout(new BoxLayout(this.panel_3, BoxLayout.X_AXIS));

	this.txpSelectedLibraryPath = new JTextPane();
	this.panel_3.add(this.txpSelectedLibraryPath);
	this.panel_1.add(this.btnSelect, "cell 1 1");

	this.lblNewLabel_2 = new JLabel("<html>&nbsp;&nbsp;&nbsp;Chose location for your library. When you import new notes to the application from your G-Note device, they will be copied here. If you use empty or non existing folder, new library will be created. If you have an existing library at the location, G-Note application will use it.</html>");
	this.lblNewLabel_2.setFont(this.lblNewLabel.getFont().deriveFont(Font.PLAIN));
	this.panel.add(this.lblNewLabel_2, "cell 0 1,growx,aligny top");

	this.panel_2 = new JPanel();
	this.panel_2.setBackground(Color.WHITE);
	this.contentPane.add(this.panel_2, BorderLayout.SOUTH);
	this.panel_2.setLayout(new MigLayout("", "[grow][]", "[]"));

	this.btnFinish = new JButton("Finish");
	this.btnFinish.addActionListener(new ActionListener() {
	    public void actionPerformed(ActionEvent arg0) {
		btnFinishActionPerformed(arg0);
	    }
	});
	this.btnFinish.setEnabled(false);
	this.panel_2.add(this.btnFinish, "cell 1 0,alignx left,aligny top");
    }

    private void validateLibraryPath() {
	File selectedFolder = new File(txpSelectedLibraryPath.getText());
	btnFinish.setEnabled(false);
	if (selectedFolder != null) {
	    if (!selectedFolder.exists())
		selectedFolder = selectedFolder.getParentFile();
	    if ((selectedFolder != null) && (selectedFolder.exists()) && (selectedFolder.isDirectory()) && (selectedFolder.canRead()) && (selectedFolder.canWrite()))
		btnFinish.setEnabled(true);
	}
    }

    protected void btnSelectActionPerformed(ActionEvent arg0) {
	String previousPath = txpSelectedLibraryPath.getText();
	JFileChooser fileChooser = new JFileChooser(txpSelectedLibraryPath.getText());
	fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
	fileChooser.showOpenDialog(this);
	File selectedFile = fileChooser.getSelectedFile();
	if (selectedFile != null)
	    txpSelectedLibraryPath.setText(selectedFile.getAbsolutePath());
	else
	    txpSelectedLibraryPath.setText(previousPath);
    }

    protected void btnFinishActionPerformed(ActionEvent arg0) {
	validateLibraryPath();
	String libraryPath = txpSelectedLibraryPath.getText();
	CreateLibraryFolder(libraryPath);
	this.appContext.setAppSettings(new ApplicationSettings());
	this.appContext.getAppSettings().setLibraryPath(libraryPath);
	try {
	    ApplicationSettingsHelper.saveApplicationSettings(AppContext.APPLICATION_SETTINGS_PATH, this.appContext.getAppSettings());
	}
	catch (Exception e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	}
	this.dispose();
    }

    private void CreateLibraryFolder(String libraryPath) {
	File libraryFolder = new File(libraryPath);
	libraryFolder.mkdirs();
    }
}
